<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 文档搜索</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        }

        body {
            background-color: #f5f5f5;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 800px;
            margin: 40px auto;
            background: white;
            border-radius: 12px;
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
            padding: 30px;
        }

        .head {
            display: flex;
            gap: 10px;
            margin-bottom: 30px;
        }

        #search-input {
            flex: 1;
            padding: 12px 16px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
            outline: none;
        }

        #search-input:focus {
            border-color: #2196f3;
            box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.1);
        }

        #search-btn {
            padding: 12px 24px;
            background-color: #2196f3;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        #search-btn:hover {
            background-color: #1976d2;
        }

        .item {
            padding: 20px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            margin-bottom: 15px;
            transition: transform 0.2s ease;
        }

        .item:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        .title {
            font-size: 18px;
            font-weight: 600;
            color: #1a1a1a;
            margin-bottom: 8px;
        }

        .desc {
            color: #666;
            margin-bottom: 12px;
            line-height: 1.5;
        }

        .item a {
            color: #2196f3;
            text-decoration: none;
            font-size: 14px;
            display: block;
            word-break: break-all;
        }

        .item a:hover {
            text-decoration: underline;
        }

        .count {
            font-size: 14px;
            color: #666;
            margin-bottom: 20px;
            padding: 12px 16px;
            background-color: #f8f9fa;
            border-radius: 6px;
            border-left: 4px solid #2196f3;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .count::before {
            content: "🔍";
            font-size: 16px;
        }

        i {
            font-style: normal;
            font-size: 14px;
            color: rgb(241, 22, 103);
            background-color: #f0f2f5;
            padding: 4px 8px;
            border-radius: 4px;
            display: inline-block;
            margin: 0 2px;
            line-height: 1.4;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        }

        @media (max-width: 600px) {
            .container {
                margin: 20px auto;
                padding: 20px;
            }

            .head {
                flex-direction: column;
            }

            #search-btn {
                width: 100%;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 实现搜索框和搜索按钮 -->
        <div class="head">
            <input type="text" id="search-input">
            <input type="button" value="搜索" id="search-btn">
        </div>
        
        <!-- 实现搜索结果拼接 -->
        <div class="body">
            <!-- 搜索结果总数 -->
            <!-- <div class="count">
                共查询出 1000 条相关结果
            </div> -->
            <!-- <div class="item">
                页面三要素：title、description、URL
                <a class = "title" href="https://www.baidu.com/" target="_blank">这是一个标题</a>
                <div class="desc">这是描述</div>
                <div class="url">https://www.baidu.com/</div>
            </div> -->
        </div>
    </div>


    <script src="./js/jquery-3.7.1.min.js"></script>

    <script>
        // 当用户点击搜索按钮时，需要获取输入框的值，并通过AJAX请求后端
        // 1、获取按钮元素，并绑定监听时间
        const btn =  document.querySelector('#search-btn');
        btn.addEventListener('click', function() {
            // alert('该按钮被点击了');
            // 2、获取输入框的值
            let input = document.querySelector('#search-input');
            if (input.value.trim() === '') {
                alert('请输入要查询的信息');
                return;
            }
            // 3、向后端发送AJAX请求
            $.ajax({
                type: "GET",
                url: "/search",
                data: {
                    "query": input.value.trim()
                },
                success(result) {
                    if (result != null && result.length != 0) {
                        fillBody(result);
                    }
                },
                error(xhr) {
                    alert('HTTP请求失败, 状态码为: '+xhr.status);
                }
            });
            // alert("AJAX请求成功");
        });


        // 将结果填充到页面上
        function fillBody(result) {
            console.log(result);
            
            // 1、先清空原始页面
            const body = document.querySelector('.body');
            let count = document.createElement('div');
            count.className = 'count';
            body.innerHTML = '';
            count.innerHTML = '';
            // 2、添加搜索结果
            count.innerHTML = '共查询出 '+ result.length + ' 条相关结果';
            body.appendChild(count);
            // 3、遍历结果数组，添加结果
            for (const element of result) {
                // element 为 每个查询结果
                let item = document.createElement('div');
                item.className = 'item';

                let title = document.createElement('a');
                title.className = 'title';
                title.href = element.url;
                title.target = '_blank';
                title.innerHTML = element.title;
                item.appendChild(title);

                let desc = document.createElement('div');
                desc.className = 'desc';
                desc.innerHTML = element.description;
                item.appendChild(desc);

                let url = document.createElement('div');
                url.className = 'url';
                url.innerHTML = element.url.replace(/\\/g, "/");
                item.appendChild(url);
                body.appendChild(item);
            }
        }
    </script>
</body>

</html>